/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.JComboBox;
import org.aswing.ListModel;

import fever.utils.Stringifier;

import fvaswing.components.input.FvInputComponent;

/**
 * Use a <strong>JComboBox</strong> as input component.
 * 
 * <p>Example using classic instanciation
 * {@code 
 *   var input : FvComboInput = new FvComboInput( );
 *   input.setListData( [ "january", "february" ] );
 *   
 *   FvInputComponentDialog.show( input, "Choose your month", this, _onInputResult );
 * }
 *  
 * <p>Example using static {@link #create()} instanciation
 * {@code 
 *   var combo : JComboBox = new JComboBox( [ "january", "february" ] );
 *   
 *   var input : FvSliderInput = FvComboInput.create( combo );
 *   
 *   FvInputComponentDialog.show( input, "Choose your year", this, _onInputResult );
 * }
 *  
 * @see fvaswing.components.FvInputComponentDialog
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.input.FvComboInput implements FvInputComponent
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _input : JComboBox;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Creates and returns a {@link FvComboInput} with passed-in {@code combobox} 
	 * component.
	 */
	public static function create( combobox : JComboBox ) : FvComboInput
	{
		var o : FvComboInput = new FvComboInput();
		o._input = combobox;
		
		return o;
	}
	
	/**
	 * Constructor.
	 */
	public function FvComboInput( )
	{
		_input = new JComboBox();
	}
	
	/**
	 * Sets the list model to provide the data to combobox.
	 */
	public function setModel( model : ListModel ) : Void
	{
		_input.setModel( model );	
	}
	
	/**
	 * Returns combobox data model.
	 */
	public function getModel() : ListModel
	{
		return _input.getModel();	
	}
	
	/**
	 * set a array to be the list data, but array is not a List Model.
	 * So when the array content was changed, you should call updateListView
	 * to update the JList(the list for combo box).But this is not a good way, its slow.
	 * So suggest you to create a ListMode eg. VectorListMode,
	 * When you modify ListMode, it will automatic update JList.
	 * 
	 * @see #setModel()
	 */
	public function setListData( data : Array ) : Void
	{
		_input.setListData( data );	
	}
	
	/** 
	 * Returns JCombobox component to add into a 
	 * {@link fvaswing.components.FvInputComponentDialog} dialog. 
	 */
	public function getContainer() : Component
	{
		return _input;
	}
	
	/**
	 * Sets combobox selected item.
	 */
	public function setValue( o : Object ) : Void
	{
		_input.setSelectedItem( o );
	}
	
	/**
	 * Sets combobox selected index.
	 */
	public function setValueIndex( index : Number ) : Void
	{
		_input.setSelectedIndex( index );
	}
	
	/**
	 * Returns combobox selected index.
	 */
	public function getValue()
	{
		return _input.getSelectedIndex();
	}
	
	/**
	 * Returns {@code true} if input is valid.
	 */
	public function isValid() : Boolean
	{
		return _input.getSelectedIndex() > -1;
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
}